package org.wzl.service.utils;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;

import java.security.InvalidParameterException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;


@Slf4j
public class JsonWrapUtil {
    private static ObjectMapper objectMapper = null;

  
    public static String toJsonString(Object object) {
        try {
            return getObjectMapper().writeValueAsString(object);
        } catch (Exception e) {
            log.error("json序列化异常", e);
            throw new InvalidParameterException("序列化错误");
        }
    }

  
    public static <T> T toObject(String json, Class<T> clazz) {
        try {
            return getObjectMapper().readValue(json, clazz);
        } catch (Exception e) {
            log.error("json序列化异常", e);
            throw new InvalidParameterException("序列化错误");
        }
    }

   
    public static <T> T toObject(String json, TypeReference<T> valueTypeRef) {
        try {
            return getObjectMapper().readValue(json, valueTypeRef);
        } catch (Exception e) {
            log.error("json序列化异常", e);
            throw new InvalidParameterException("序列化错误");
        }
    }


  
    private static ObjectMapper getObjectMapper() {
        if (objectMapper != null) {
            return objectMapper;
        }
        synchronized (JsonWrapUtil.class) {
            if (objectMapper == null) {
                JsonFactory factory = new JsonFactory();
                //允许没有引号的字段
                factory.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
                objectMapper = new ObjectMapper(factory);
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

                JavaTimeModule timeModule = new JavaTimeModule();
                // 设置LocalDateTime的序列化格式
                timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyyMMdd")));
                timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HHmmss")));
                objectMapper.registerModule(timeModule);

            }
        }
        return objectMapper;
    }

    public static <T> T mapToObject(Map<String, String> map, TypeReference<T> valueTypeRef) {
        return toObject(toJsonString(map), valueTypeRef);
    }


    public static <T, R> T objectToMap(R sapEmployeeShiftCommit, TypeReference<T> valueTypeRef) {
        return toObject(toJsonString(sapEmployeeShiftCommit), valueTypeRef);
    }

}
